package cn.neusoft.controller;

import cn.neusoft.domain.Commodity;
import cn.neusoft.domain.CommodityClass;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.query.CommodityClassQuery;
import cn.neusoft.query.CommodityQuery;
import cn.neusoft.service.CommodityClassService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/commodity_class")
@Api(value = "商品分类模块的API",description="商品分类相关的CRUD功能")
public class CommodityClassController {

    @Autowired
    CommodityClassService commodityClassService;

    @GetMapping("/all")
    @ApiOperation(value = "查看所有商品分类信息" )
    public ResultBean getAll(){
        List<CommodityClass> c = commodityClassService.getAll();
        return ResultBean.success(c);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加商品分类" )
    public ResultBean add(@RequestBody CommodityClass c){
        c.setId(String.valueOf(UUID.randomUUID()));
        commodityClassService.add(c);
        return ResultBean.success();
    }

    @DeleteMapping("/del/{id}")   // Delete + /stu/1
    @ApiOperation(value = "根据id删除商品分类" )
    public ResultBean delete(@PathVariable("id") Integer id){

        if(id==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        commodityClassService.delete(id);
        return ResultBean.success();
    }

    @PutMapping("/mod")   //   Put + /stu
    @ApiOperation(value = "修改商品分类" )
    public ResultBean update(@RequestBody CommodityClass c){

        if(c==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        if(c.getId()==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        commodityClassService.update(c);
        return ResultBean.success();
    }

    @GetMapping("/get/{id}")    // Get + /stu/1
    @ApiOperation(value = "通过id查询商品分类信息" )
    public ResultBean getById(@PathVariable("id") Integer id){

        if(id==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        CommodityClass c = commodityClassService.getById(id);
        return ResultBean.success(c);
    }

    @GetMapping("/getByName/{name}")
    @ApiOperation(value = "根据名字查询商品信息" )
    public List<CommodityClass> getSupplierByName(@PathVariable String name) {
        return commodityClassService.getByName(name);
    }

    @GetMapping("/page/{page}/{pageSize}")
    public List<CommodityClass> getSuppliersByPage(@PathVariable int page, @PathVariable int pageSize) {
        return commodityClassService.getCommodityClassesByPage(page,pageSize);
    }

    @GetMapping("/totalCount")
    public int getTotalCount() {
        return commodityClassService.getTotalCount();
    }

    @PostMapping("/page")  //  Post + /customer/page
    @ApiOperation(value = "分页查询商品信息" )
    public ResultBean findByPage(@RequestBody CommodityClassQuery commodityClassQuery){
        PageBean<CommodityClass> pageBean = commodityClassService.findByPage(commodityClassQuery);
        return ResultBean.success(pageBean);
    }

    @DeleteMapping("/{first_class}")
    public void deleteByFirstClass(@PathVariable String first_class) {
        commodityClassService.deleteByFirstClass(first_class);
    }

    @DeleteMapping("/second/{first_class}")
    public void deleteByFirstClassSecond(@PathVariable String first_class) {
        commodityClassService.deleteByFirstClassSecond(first_class);
    }
}
